package ru.gerunn.silent.io;

import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.net.ssl.SSLSocket;

import ru.gerunn.silent.exception.SilentWriteMessageException;
import ru.gerunn.silent.exception.SilentWriterException;
import ru.gerunn.silent.message.Message;

public class SocketWriter implements Writer {

	ObjectOutputStream writer;

	public SocketWriter(SSLSocket socket) throws SilentWriterException {
		try {
			this.writer = new ObjectOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			throw new SilentWriterException("Couldn't create writer", e);
		}
	}

	public void write(Message message) throws SilentWriteMessageException {
		try {
			this.writer.writeObject(message);
			this.writer.flush();
		} catch (IOException e) {
			throw new SilentWriteMessageException("IOException on writing message", e);
		}
	}

}
